package minio

import (
	"context"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"github.com/sirupsen/logrus"
	"github.com/tluo-github/super-runner/common"
)

var instance *MinioHelper

type MinioHelper struct {
	minioConfig common.MinioConfig
	minioClient *minio.Client
}

func (mh *MinioHelper) PutObject(objectName string, filePath string) (minio.UploadInfo,error){
	return mh.minioClient.FPutObject(context.Background(), mh.minioConfig.Bucket, objectName, filePath, minio.PutObjectOptions{ContentType: "application/text"})
	//
	//uploadInfo, err := mh.minioClient.FPutObject(context.Background(), mh.minioConfig.Bucket, objectName, filePath, minio.PutObjectOptions{ContentType: "application/text"})
	//if err != nil {
	//	logrus.WithFields(logrus.Fields{
	//		"objectName": objectName,
	//	}).Error("PutObject with error: %v", err)
	//}
	//logrus.WithFields(logrus.Fields{
	//	"objectName": objectName,
	//}).Info("PutObject ok")
	//return uploadInfo
}

func NewMinioHelper(minioConfig common.MinioConfig) *MinioHelper {
	if instance == nil {

		client, err := minio.New(minioConfig.Url, &minio.Options{Creds: credentials.NewStaticV4(minioConfig.Access_key, minioConfig.Secret_key, "")})
		if err != nil{
			logrus.Errorln(err)
			return nil
		}
		instance = &MinioHelper{minioClient: client}
		instance.minioConfig = minioConfig
	}
	return instance
}